﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Net;
using System.Text;
using System.IO;
using ProjetoClasse.Model.Entidade;

namespace Juntae.Util
{
    public static class FacebookUtil
    {
        public const string CLIENTEID = "204094279626024";
        public const string APPSECRET = "f62a2b109c779f32d01b801584dbdbda";

        public static void inviteFriends(string userToken, List<FacebookUsersDTO> listaUsuarios)
        {
            try
            {
                WebClient wc = new WebClient();
                wc.Encoding = Encoding.UTF8;
                foreach (FacebookUsersDTO f in listaUsuarios)
                {
                    wc.UploadString(@"https://graph.facebook.com/" + f.id + "/feed", null, "access_token=" + userToken + "&message=Testanddo http://www.juntae.com.br acesse já!!");
                }
            }
            catch (System.Net.WebException ex)
            {
                StreamReader readStream = new StreamReader(ex.Response.GetResponseStream());
                throw new Exception(readStream.ReadToEnd());
            }
        }

        public static string getUserToken(string code)
        {
            WebRequest request = WebRequest.Create(new Uri(@"https://graph.facebook.com/oauth/access_token?redirect_uri=http://localhost:49601/Users/WizardCadastro.aspx&client_id=" + FacebookUtil.CLIENTEID + "&code=" + code + "&client_secret=" + FacebookUtil.APPSECRET));
            request.Method = "GET";
            WebResponse response = request.GetResponse();
            Stream dataStream = response.GetResponseStream();
            StreamReader sr = new StreamReader(dataStream);
            string responseFromServer = sr.ReadToEnd();
            sr.Close();
            dataStream.Close();
            response.Close();
            return responseFromServer;
        }

        public static List<FacebookUsersDTO> getAllUsers(string userToken)
        {
            throw new NotImplementedException();
        }
    }
}